package com.hxzy.shop.config;

import cn.hutool.core.util.ObjectUtil;
import com.hxzy.shop.cachedao.AuthCacheService;
import com.hxzy.shop.constant.AuthConstant;
import com.hxzy.shop.dto.UserDto;
import com.hxzy.shop.utils.WebUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * feign 请求头处理
 *
 * @author Administrator
 */
@Component
public class FeignInterceptor implements RequestInterceptor {
    private static final String MERCHANT_ID = "merchantId";
    @Resource
    private AuthCacheService service;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        String token = WebUtil.getRequest().getHeader(AuthConstant.JWT_TOKEN_HEADER);
        UserDto backUserInfo = service.getBackUserInfo(token);
        if (ObjectUtil.isNotEmpty(backUserInfo)) {
            requestTemplate.header(MERCHANT_ID, Long.toString(backUserInfo.getMerchantId()));
        } else {
            requestTemplate.header(MERCHANT_ID, WebUtil.getRequest().getHeader(MERCHANT_ID));
        }
    }
}